package cn.xyafu.lost.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.FragmentActivity;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import cn.xyafu.lost.R;
import cn.xyafu.lost.activity.UserLostActivity;
import cn.xyafu.lost.activity.UserNewsActivity;
import cn.xyafu.lost.dialog.MineDialog;
import cn.xyafu.lost.utils.LostOpenHelper;

/**
* @author wangdh
* @time 2021/12/7 0007
* desc：  我的Fragment
*/
public class MineFragment extends BaseFragment implements View.OnClickListener {

    private TextView tv_user_name;
    private TextView tv_user_message;
    private TextView tv_user_lost;
    private TextView tv_user_mail;

    private FragmentActivity mContent;

    LostOpenHelper openHelper;

    private String uid;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContent = getActivity();
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        // 初始化组件
        initView(view);
        // 初始化监听器
        initListener();
        // 初始化用户名显示
        showUsername();


        return view;

    }

    // 初始化用户名显示
    // 从数据库中获取数据信息
    private void showUsername(){
        String username = "";
        openHelper = new LostOpenHelper(getContext(),"lostdb",null,1);
        SQLiteDatabase db = openHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from t_user", null);
        cursor.moveToFirst();	//移至第一行
        for(int i = 0; i < cursor.getCount(); i++){	//遍历每行数据
            // 获取列数据
            username =  cursor.getString(1);
            uid = cursor.getString(6);
        }
        tv_user_name.setText(username);
    }

    // 初始化组件
    private void initView(View v){
        tv_user_name = v.findViewById(R.id.tv_user_name);
        tv_user_message = v.findViewById(R.id.tv_user_message);
        tv_user_lost = v.findViewById(R.id.tv_user_lost);
        tv_user_mail = v.findViewById(R.id.tv_user_mail);
    }

    // 初始化监听事件
    private void initListener() {
        // 需求: 点击底部布局,然后进行切换fragment
        tv_user_message.setOnClickListener(this);
        tv_user_lost.setOnClickListener(this);
        tv_user_mail.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch(v.getId()){
            case R.id.tv_user_message:  // 个人信息
                MineDialog dialog = new MineDialog(mContent);
                dialog.setTitle("个人信息");
                WindowManager.LayoutParams wlp =dialog.getWindow().getAttributes();wlp.gravity = Gravity.TOP | Gravity.LEFT;
                wlp.x=10;
                wlp.y=240;
                dialog.show();
                dialog.getWindow().setLayout(720,480);
                break;
            case R.id.tv_user_lost:     // 已发布失物信息
                Intent intent = new Intent(mContent, UserLostActivity.class);
                intent.putExtra("uid",uid);
                startActivityForResult(intent,1);
                break;
            case R.id.tv_user_mail:     // 收到的消息
                Intent intent1 = new Intent(mContent, UserNewsActivity.class);
                intent1.putExtra("uid",uid);
                startActivityForResult(intent1,3);
                break;
        }

    }
}